
import time

from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver

class Base():
    def __init__(self,driver):
        self.driver = driver

    def  goto(self,url):
        self.driver.get(url)

    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda  driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)

        except Exception as e:
            t = time.strftime("%Y_%m_%d_%H_%M_%S")
            time.localtime()
            self.driver.save_screenshot(t+'.png')
            print(e)
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)

    def click(self,loc):
        self.find_element(loc).click()

    #滚动事件
    def scrollTo(self,x):
        self.driver.execute_script(f'window.scrollTo(0,{x})')
    #弹框处理
    def alert(self):
        self.driver.switch_to.alert.accept()
    #iframe处理
    def frame(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))
    #切换页面
    def new_widow(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
    #鼠标事件
    def shubiao(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).perform()
    #键盘事件
    def jianpan(self):
        self.driver.send_keys(Keys.ENTER)
    #浏览器事件
    def liu(self):
        self.driver.refresh()
    #下拉框选择
    def index(self):
        index = Select(self.driver.find_element())
        index.select_by_index(2)